package com.ly.sy.util;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.5.3</version>
</dependency>
*/
/**
 * 这是一个阿里云短信服务接口工具
 * 梁瑜
 * 2020.11.17
 * **/
public class SendSms {
    private static final String SignName = "sy课堂";
    private static final String TemplateCode = "SMS_205396506";
    //LTAI4GAKBF1CTWDEf6kqN7V7
    //NsPrj9vxTQO1LAhAo3IiaGSXiSXZdw

    public static void sendShortMessage(String randomCode,String phoneNum) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GAKBF1CTWDEf6kqN7V7", "NsPrj9vxTQO1LAhAo3IiaGSXiSXZdw");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", SignName);
        request.putQueryParameter("TemplateCode", TemplateCode);
//      随机生成的验证码让接口发送到手机
        request.putQueryParameter("TemplateParam","{\"code\":"+"'"+randomCode+"'"+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println("阿里云服务端返回的信息："+response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
    //测试
//    public static void main(String[] args) {
//        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GAKBF1CTWDEf6kqN7V7", "NsPrj9vxTQO1LAhAo3IiaGSXiSXZdw");
//        IAcsClient client = new DefaultAcsClient(profile);
//
//        CommonRequest request = new CommonRequest();
//        request.setSysMethod(MethodType.POST);
//        request.setSysDomain("dysmsapi.aliyuncs.com");
//        request.setSysVersion("2017-05-25");
//        request.setSysAction("SendSms");
//        request.putQueryParameter("RegionId", "cn-hangzhou");
//        request.putQueryParameter("PhoneNumbers", "15678475291");
//        request.putQueryParameter("SignName", SignName);
//        request.putQueryParameter("TemplateCode", TemplateCode);
////      随机生成的验证码让接口发送到手机request.putQueryParameter("TemplateParam", "{\"code\":"+"'"+code+"'"+"}");
//        request.putQueryParameter("TemplateParam","{code:'012345'}");
//        try {
//            CommonResponse response = client.getCommonResponse(request);
//            System.out.println("阿里云服务端返回的信息："+response.getData());
//        } catch (ServerException e) {
//            e.printStackTrace();
//        } catch (ClientException e) {
//            e.printStackTrace();
//        }
//    }
}
